<?php

namespace haibao\app\data;

class ColumnCategory extends \haibao\app\data\BaseMysql{

	public function __construct(){
		parent::__construct('\haibao\app\model\data\ColumnCategory');
	}
	
	//CMS后台	
	/**
	 * 得到所有正常栏目列表(缓存)
	 */
	public function getColumn(){
		$cache = new \haibao\app\cache\Column();
		$columnList = json_decode($cache->getCacheByKey(\haibao\app\model\data\ColumnCategory::CACHE_KEY),true);
		if(empty($columnList)){
			$columnList = $this->setColumnCache();
		}
		return $columnList;
	}
	
	/**
	 * 得到根据父id得到其子栏目
	 */
	public function getColumnByParentId($parentId){
		$sql = "select * from `ColumnCategory` where ParentId = %s and Status = %s";
		$result = $this->query($sql,array($parentId,\haibao\app\model\data\ColumnCategory::STATUS_YES));
		$columnList = array();
		$list = array();
		if(!empty($result)){
			while($result->fetch_assoc()){
				$list['Id'] = $result->getData('Id');
				$list['Name'] = $result->getData('Name');
				$list['ParentId'] = $result->getData('ParentId');
				array_push($columnList, $list);
			}
		}
		return $columnList;
		
	}
	
	/**
	 * 增加栏目
	 */
	public function addColumn($columnData){
		$this->add($columnData);
		$this->setColumnCache();
		$this->setTopColumnList();
	}
	
	/**
	 * 获取最大栏目sort
	 */
	public function getColumnMaxSort(){
		$sql = "select max(sort) as maxSort from ColumnCategory where `Status` = %s";
		return $this->query($sql,array(\haibao\app\model\data\ColumnCategory::STATUS_YES))->fetch_assoc();
	}
	
	/**
	 * 修改栏目状态
	 * @return unknown
	 */
	public function updateColumnStatus($id, $status = \haibao\app\model\data\ColumnCategory::STATUS_DELETE){
		$this->updateById($id, array(\haibao\app\model\filter\ColumnCategory::CONDITION_FIELD_NAME_STATUS=>$status));
		$this->setColumnCache();
		$this->setTopColumnList();
	}
	
	
	
	/**
	 * 修改栏目功能状态
	 */
	public function changeStatus($status = \haibao\app\model\data\ColumnCategory::STATUS_YES){
		$cache = new \haibao\app\cache\Column();
		$cache->setColumnStatusCache((int)$status);
	}
	
	public function getIsShowColumn(){
		$cache = new \haibao\app\cache\Column();
		return $cache->getColumnStatus();
	}
	
	/**
	 * 修改栏目排序
	 */
	public function updateColumnSort($id, $arrSet){
		$this->updateById($id, $arrSet);
		$this->setColumnCache();
		$this->setTopColumnList();
	}
	
	/**
	 * 取可用栏目列表
	 * @param boolean $isReturn 是否需要返回值
	 */
	private function setColumnCache(){
		$columnList = array();
		$result = $this->query("select * from ColumnCategory where Status <> 2 ORDER BY Sort");
		$list = array();
		while($result->fetch_assoc()){
			$list['Id'] = $result->getData('Id');
			$list['Name'] = $result->getData('Name');
			$list['ParentId'] = $result->getData('ParentId');
			$columnList[] = $list;
		}
		$cache = new \haibao\app\cache\Column();
		$cache->setCache(\haibao\app\model\data\ColumnCategory::CACHE_KEY, json_encode($columnList,true));
		return $columnList;
	}

	
	//API接口
	/**
	 * 得所有顶级栏目
	 */
	public function getTopColumnList(){
		$cache = new \haibao\app\cache\Column();
		$topColumnList = json_decode($cache->getCacheByKey(\haibao\app\model\data\ColumnCategory::CACHE_API_KEY),true);
		if(empty($topColumnList)){
			$topColumnList = $this->setTopColumnList();
		}
		return $topColumnList;
	}
	
	public function getColumnByName($name){
		return $this->query('select Id,Name from ColumnCategory where Name=%s and Status<>%s and ParentId<>%s limit 1', array(
			$name, \haibao\app\model\data\ColumnCategory::STATUS_DELETE, 0
		))->fetch_assoc();
	}
	
	private function setTopColumnList(){
		$topColumnList = array();
		$result = $this->query("select Id,Name from ColumnCategory where ParentId=0 and Status = 1 ORDER BY Sort");
		$list = array();
		while($result->fetch_assoc()){
			$list['Id'] = $result->getData('Id');
			$list['Name'] = $result->getData('Name');
			$topColumnList[] = $list;
		}
		$cache = new \haibao\app\cache\Column();
		$cache->setCache(\haibao\app\model\data\ColumnCategory::CACHE_API_KEY, json_encode($topColumnList,true));
		return $topColumnList;
	}
}